﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using KegCommunication.Communication;

namespace KegCommunication
{
    public class Sender
    {
        // Use a default pipe name. Be careful not to reuse
        // this on other applications.
        public static void SendMessage(List<string> messages)
        {
            SendMessage(messages, Receiver.DefaultPipeName);
        }

        // Use this method when we have an actual pipe name.
        public static void SendMessage(List<string> messages, string PipeName)
        {
            EndpointAddress ep = new EndpointAddress(string.Format("{0}/{1}", PipeService.URI, PipeName));
            
            // TODO: Convert to HttpBinding
            // IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel( new BasicHttpBinding(), ep );
            IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel( new NetNamedPipeBinding(), ep );
            proxy.PipeIn( messages );
        }

        public static void NotifyStartPour(PourEventArgs pe)
        {
            EndpointAddress ep = new EndpointAddress(string.Format("{0}/{1}", PipeService.URI, Receiver.DefaultPipeName));

            IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel(new NetNamedPipeBinding(), ep);

            try
            {
                proxy.NotifyStartPour(pe);
            }
            catch (EndpointNotFoundException)
            {
            }
        }

        public static void NotifyContinuePour(PourEventArgs pe)
        {
            EndpointAddress ep = new EndpointAddress(string.Format("{0}/{1}", PipeService.URI, Receiver.DefaultPipeName));

            IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel(new NetNamedPipeBinding(), ep);
            try
            {
                proxy.NotifyContinuePour(pe);
            }
            catch (EndpointNotFoundException)
            {
            }
        }

        public static void NotifyEndPour(PourEventArgs pe)
        {
            EndpointAddress ep = new EndpointAddress(string.Format("{0}/{1}", PipeService.URI, Receiver.DefaultPipeName));

            IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel(new NetNamedPipeBinding(), ep);
            
            try
            {
                proxy.NotifyEndPour(pe);
            }
            catch (EndpointNotFoundException)
            {
            }
        }

        public static void NotifyUpdateTemperature(TemperatureEventArgs te)
        {
            EndpointAddress ep = new EndpointAddress(string.Format("{0}/{1}", PipeService.URI, Receiver.DefaultPipeName));

            IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel(new NetNamedPipeBinding(), ep);

            try
            {
                proxy.NotifyUpdateTemperature(te);
            }
            catch (EndpointNotFoundException)
            {
            }
        }

        public static void NotifyUserAuthenticated(UserAuthenticatedEventArgs ue)
        {
            EndpointAddress ep = new EndpointAddress(string.Format("{0}/{1}", PipeService.URI, Receiver.DefaultPipeName));

            IPipeService proxy = ChannelFactory<IPipeService>.CreateChannel(new NetNamedPipeBinding(), ep);

            try
            {
                proxy.NotifyUserAuthenticated(ue);
            }
            catch (EndpointNotFoundException)
            {
            }
        }
    }
}



